using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Zsnd.Helpers;
using MaymuseMS.DAL;
using MaymuseMS.Models;

namespace MaymuseMS.Controllers {
    public class SupplierUserController : GenericController {

        public ActionResult Index() {
            return View();
        }

        [HttpPost]
        public ActionResult Index(JQGridParm parm, int supplierId) {
            string sidx = parm.Sidx;
            string sord = parm.Sord;
            int page = parm.Page;
            int rows = parm.Rows;

            int pageIndex = Convert.ToInt32(page) - 1;
            int pageSize = rows;
            int totalRecords = unitOfWork.SupplierUserRepository.dbSet.Count();
            int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);

            var entities = unitOfWork.SupplierUserRepository.Get(pageIndex, pageSize, sidx, sord, parm.Filters, n => n.SupplierId == supplierId);

            var enums = entities.Select(e => new {
                id = e.SupplierUserId,
                cell = new object[] { 
                    e.Name,
                    e.Department,
                    e.Post,
                    e.Phone,
                    e.Telephone,
                    e.Email,
                    e.Remark
                }
            });

            var jsonData = new {
                success = true,
                total = totalPages,
                page,
                records = totalRecords,
                rows = enums
            };
            return Json2(jsonData);
        }

        [HttpPost]
        public ActionResult Add(SupplierUser supplieruser, int supplierId) {
            SupplierUser supplieruserAdd = new SupplierUser();
            if (TryUpdateModel(supplieruserAdd)) {
                supplieruserAdd.SupplierId = supplierId;
                unitOfWork.SupplierUserRepository.Add(supplieruserAdd);
                unitOfWork.Save();
                return Json2(new { success = true });
            }
            return Json2(new { success = false, message = errorMessage });
        }

        [HttpPost]
        public ActionResult Edit(int? id, SupplierUser supplieruser) {
            if (id != null) {
                SupplierUser supplieruserEdit = unitOfWork.SupplierUserRepository.Get((int)id);
                if (supplieruserEdit != null) {
                    if (TryUpdateModel(supplieruserEdit)) {
                        unitOfWork.Save();
                        return Json2(new { success = true });
                    }
                }
            }
            return Json2(new { success = false, message = errorMessage });
        }

        [HttpPost]
        public ActionResult Delete(string id) {
            if (string.IsNullOrWhiteSpace(id))
                Json2(new { success = false, message = errorMessage });
            try {
                unitOfWork.SupplierUserRepository.Remove(id);
                unitOfWork.Save();
                return Json2(new { success = true });
            } catch (ArgumentNullException e) {
                return Json2(new { success = false, message = e.Message });
            } catch (Exception e) {
                return Json2(new { success = false, message = errorMessage });
            }
        }

        public ActionResult SupplierUsers() {
            var supplierusers = unitOfWork.SupplierUserRepository.dbSet.Select(s => new { key = s.SupplierUserId, value = s.Name }).AsEnumerable();
            var jsonData = new { value = supplierusers };
            return Json2(jsonData);
        }
    }
}

